package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.request.ExpenseRequest;
import cn.com.thinvent.dispatch.entity.response.ExpenseResponse;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.entity.response.ReturnModel;
import cn.com.thinvent.dispatch.service.biz.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/expenses")
public class ExpenseController {
    @Autowired
    private ExpenseService expenseService;

    @GetMapping
    public ReturnModel getExpenses(@RequestParam(defaultValue = "1") int page,
                                                   @RequestParam(defaultValue = "10") int pageSize,
                                                   @RequestParam(defaultValue = "") String expenseName) {
        PagedGridResult<ExpenseResponse> expenses = expenseService.getExpenses(page, pageSize, expenseName);
        return new ReturnModel(200, expenses, "Success");
    }

    @PostMapping
    public ReturnModel addExpense(@RequestBody ExpenseRequest expense) {
        Integer result = expenseService.addExpense(expense);
        return new ReturnModel(200, result, "Success");
    }

    @PutMapping("/{id}")
    public ReturnModel updateExpense(@PathVariable String id, @RequestBody ExpenseRequest expense) {
        Integer result = expenseService.updateExpense(id, expense);
        return new ReturnModel(200, result, "Success");
    }

    @DeleteMapping("/{id}")
    public ReturnModel deleteExpense(@PathVariable String id) {
        Integer result = expenseService.deleteExpense(id);
        return new ReturnModel(200, result, "Success");
    }

    @GetMapping("/{id}")
    public ReturnModel getExpenseDetails(@PathVariable String id) {
        ExpenseResponse expense = expenseService.getExpenseDetails(id);
        return new ReturnModel(200, expense, "Success");
    }

}
